package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.BookMapper;
import com.itheima.pojo.Book;
import com.itheima.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IBookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Autowired
    private  BookMapper bookMapper;


    @Override
    public List<Book> initList(long current,long size,String typeinput,String nameinput) {
        IPage<Book> page = new Page<>(current,size);
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        if (typeinput == "" && nameinput == ""){
            page = bookMapper.selectPage(page, null);
        } else {
            if (typeinput != ""){
                lqw.like(Book::getType,typeinput);
            }
            if (nameinput != ""){
                lqw.like(Book::getName,nameinput);
            }
            page = bookMapper.selectPage(page,lqw);
        }
        return page.getRecords();
    }

    @Override
    public IPage<Book> initListPage(long current, long size, String typeinput, String nameinput) {
        IPage<Book> page = new Page<Book>(current,size);
        if (typeinput == "" && nameinput == ""){
            page = bookMapper.selectPage(page, null);
        } else {
            LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
            //1.自己写逻辑
            if (typeinput != ""){
                lqw.like(Book::getType,typeinput);
            }
            if (nameinput != ""){
                lqw.like(Book::getName,nameinput);
            }
            page = bookMapper.selectPage(page, lqw);
        }
        return page;
    }
}
